use std::cmp::Ordering;

struct Solution;

impl Solution {
    /// 最简单的就是三个循环解决问题
    /// 优化 拆开成一个两循环和一个单循环
    /// max 从后往前 0<=i<j<k r[i]<r[j] r[j]<r[m]
    pub fn num_teams(rating: Vec<i32>) -> i32 {
        let len = rating.len();

        if len < 3 {
            return 0;
        }

        let mut res = 0;

        for (i, val1) in rating[1..(len - 1)].iter().enumerate() {
            let mut cmp = (0, 0, 0, 0);
            for (j, val2) in rating.iter().enumerate() {
                match val1.cmp(val2) {
                    Ordering::Less => {
                        if i + 1 < j {
                            cmp.1 += 1;
                        } else {
                            cmp.0 += 1;
                        }
                    }
                    Ordering::Greater => {
                        if i + 1 < j {
                            cmp.3 += 1;
                        } else {
                            cmp.2 += 1;
                        }
                    }
                    _ => {}
                }
            }
            // println!("val {} {} {} {} {} ", val1, cmp.0, cmp.1, cmp.2, cmp.3);
            res += cmp.0 * cmp.3 + cmp.1 * cmp.2;
        }
        res
    }
}

pub fn count_number_of_teams(rating: Vec<i32>) -> i32 {
    Solution::num_teams(rating)
}


fn main() {

}


#[test]
fn test() {
    let vec = vec![2, 5, 3, 4, 1];
    let res = count_number_of_teams(vec);
    println!("{}", res);
}
